package com.sdut.material.controller;


import com.sdut.material.pojo.Material;
import com.sdut.material.service.IMaterialService;
import com.sdut.material.util.JSONResult;
import com.sdut.material.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Yu
 * @since 2022-04-20
 */
@Controller
@RequestMapping("/material")
public class MaterialController {
    @Autowired
    private IMaterialService materialService;
    @RequestMapping("/selectById")
    @ResponseBody
    public JSONResult selectById (Integer materialId) {
        Material material = materialService.selectById(materialId);
        return JSONResult.ok(material);
    }
    @RequestMapping("/getMaterialListPage")
    public String getMaterialListPage() {
        return "material/material_list";
    }
    @RequestMapping("/selectByPage")
    @ResponseBody
    public LayUITableResult selectByPage(long page,long limit) {
        return materialService.selectByPage(page,limit);
    }
    @RequestMapping("/getMaterialInsertPage")
    public String getMaterialInsertPage() {
        return "material/material_insert";
    }
    @RequestMapping("/insert")
    @ResponseBody
    public JSONResult insert(Material material,String types) {
        return materialService.insert(material,types);
    }
    @RequestMapping("/getMaterialUpdatePage")
    public String getMaterialUpdatePage() {
        return "material/material_update";
    }
    @RequestMapping("/selectMaterialAndTypes")
    @ResponseBody
    public JSONResult selectMaterialAndTypes(Integer materialId) {
        return materialService.selectMaterialAndTypes(materialId);
    }
    @RequestMapping("/update")
    @ResponseBody
    public JSONResult update(Material material,String types) {
        return materialService.updateMaterial(material,types);
    }
    @RequestMapping("/deleteById")
    @ResponseBody
    public JSONResult deleteById(Integer materialId) {
        materialService.deleteById(materialId);
        return JSONResult.ok("删除成功");
    }
    @RequestMapping("/deleteAll")
    @ResponseBody
    public JSONResult deleteAll(String ids) {
        String[] materialIds = ids.split(",");
        for (String id : materialIds) {
            materialService.deleteById(Integer.parseInt(id));
        }
        return JSONResult.ok("删除成功");
    }

}

